--use [anton]
---------------------------------------
CREATE TABLE [dbo].[history](
	[record_desc] [nchar](100) NULL,
	[record_id] [int] NOT NULL,
	[record_date] [datetime] NOT NULL,
	[record_goods_id] [int] NOT NULL,
	[record_amount] [int] NOT NULL,
	[record_income_flag] [bit] NOT NULL,
	[record_name] [int] NOT NULL,
 CONSTRAINT [PK_history_1] PRIMARY KEY ([record_id] ASC
)
----------------------------------------
CREATE TABLE [dbo].[goods](
	[id] [int] NOT NULL,
	[place_id] [int] NULL,
	[name] [int] NOT NULL,
 CONSTRAINT [PK_goods] PRIMARY KEY(	[id] ASC )
----------------------------------------
CREATE TABLE [dbo].[warehouse](
	[place] [int] NOT NULL,
	[state] [int] NULL,
 CONSTRAINT [PK_warehouse] PRIMARY KEY (	[place] ASC)
-----------------------------------------
CREATE TABLE [dbo].[class](
	[node_id] [int] NOT NULL,
	[parent_node_id] [int] NULL,
	[class_name] [nchar](100) NOT NULL,
	[is_item] [bit] NOT NULL,
	[is_unit] [bit] NOT NULL,
	[max_value] [int] NOT NULL,
	[desc] [nvarchar](max) NULL,
 CONSTRAINT [PK_class_1] PRIMARY KEY(	[node_id] ASC)
 
-----------------------------------------
ALTER TABLE [dbo].[class]  WITH CHECK ADD  CONSTRAINT [FK_class_class] FOREIGN KEY([parent_node_id])
REFERENCES [dbo].[class] ([node_id])

ALTER TABLE [dbo].[history]  WITH CHECK ADD  CONSTRAINT [FK_history_goods] FOREIGN KEY([record_goods_id])
REFERENCES [dbo].[goods] ([id])

ALTER TABLE [dbo].[goods]  WITH CHECK ADD  CONSTRAINT [FK_goods_class] FOREIGN KEY([name])
REFERENCES [dbo].[class] ([node_id])

ALTER TABLE [dbo].[goods]  WITH CHECK ADD  CONSTRAINT [FK_goods_warehouse] FOREIGN KEY([place_id])
REFERENCES [dbo].[warehouse] ([place])

----------------------------------------
ALTER TABLE [dbo].[class] ADD CONSTRAINT uc_names UNIQUE ([class_name])
----------------------------------------